from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from django.utils import timezone
from users.models import User
from orders.models import OrderInfo
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.home_serializers import GoodsVisitCountSerializer
from datetime import timedelta


class UserTotalCountView(APIView):
    """ 用户总量统计 """

    def get(self, request):
        now_time_date = timezone.localtime().date()
        user_count = User.objects.count()

        return Response({"count": user_count, "date": now_time_date})


class UserDayCountView(APIView):
    """ 日增用户统计 """

    def get(self, request):
        # datetime.datetime(2021, 5, 9, 17, 48, 40, 312594, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
        zero_time = timezone.localtime().replace(hour=0, minute=0, second=0, microsecond=0)
        now_time_date = timezone.localtime().date()
        user_count = User.objects.filter(date_joined__gte=zero_time).count()

        return Response({"count": user_count, "date": now_time_date})


class UserActiveCountView(APIView):
    """ 日活跃用户统计 """

    def get(self, request):
        zero_time = timezone.localtime().replace(hour=0, minute=0, second=0, microsecond=0)
        now_time_date = timezone.localtime().date()
        user_count = User.objects.filter(last_login__gte=zero_time).count()

        return Response({"count": user_count, "date": now_time_date})


class UserOrderCountView(APIView):
    """ 日下单用户量统计 """

    def get(self, request):
        now_time_date = timezone.localtime().date()
        zero_time = timezone.localtime().replace(hour=0, minute=0, second=0, microsecond=0)

        # 方案二：主表入手查询 --> 使用从表的已知条件，直接查询主表数据;orders是从表关联主表字段的related_name属性
        users_model = User.objects.filter(orders__create_time__gte=zero_time)
        user_count = len(set(users_model))  # 强转集合去重

        # 方案一：从表入手查询
        # orders_model = OrderInfo.objects.filter(create_time__gte=zero_time)
        # user_set = set()
        # for order_model in orders_model:
        #     user_set.add(order_model.user)
        # user_count = len(user_set)

        return Response({"count": user_count, "date": now_time_date})


class UserMonthCountView(APIView):
    """ 月增用户统计 """

    def get(self, request):
        now_time = timezone.localtime()
        begin_time = (now_time - timedelta(days=29)).replace(hour=0, minute=0, second=0, microsecond=0)

        date_list = []
        for i in range(30):
            zero_time = begin_time + timedelta(days=i)
            nextday_time = zero_time + timedelta(days=1)
            user_count = User.objects.filter(date_joined__gte=zero_time, date_joined__lt=nextday_time).count()
            date_list.append({"count": user_count, "date": zero_time.date()})

        return Response(date_list)


class GoodsDayView(ListAPIView):
    """ 日分类商品访问量 """
    queryset = GoodsVisitCount.objects.all()
    serializer_class = GoodsVisitCountSerializer

    def get_queryset(self):
        zero_time = timezone.localtime().replace(hour=0, minute=0, second=0, microsecond=0)
        ret = self.queryset.filter(create_time__gte=zero_time)
        return ret
